package de.reichelt.fx;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javafx.geometry.Pos;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.util.Callback;

public class FxTable {
  private List<FxTableField> columns;
  private String tableName; 
  private TableView table;
  private Logger log;
  private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
  private NumberFormat nfmt = new DecimalFormat("#,##0.00");
  
  
  public FxTable(String tableName, TableView table) {
    log = Logger.getLogger(this.getClass().getName());
    log.fine("erzeuge FxTable");
    this.columns = new ArrayList<>();
    this.tableName = tableName;
    this.table = table;
  }

  public List<FxTableField> getColumns() {
    return columns;
  }

  public String getTableName() {
    return tableName;
  }

  public TableView getTable() {
    return table;
  }

  public FxTableField getColumn(String fieldName) {
    for (FxTableField f: columns) {
      if (f.getFieldName().equalsIgnoreCase(fieldName)) return f;
    }
    return null;
  }
  
  public TableColumn addColumn(FxTableField column) {
    log.fine("erzeuge neue Spalte '"+column.getFieldName()+"'");
    this.columns.add(column);
    log.fine("addField(): "+column.getFieldName());
    TableColumn col = new TableColumn(column.getLabel());
    col.setPrefWidth(column.getWidth());
    col.setCellValueFactory(column.getPvf());
    if (column.getFieldType()==FxTableField.TYPE_DATE) {
      col.setCellFactory(new Callback<TableColumn, TableCell>() {
        @Override
        public TableCell call(TableColumn param) {
          return new TableCell() {
            @Override
            protected void updateItem(Object item, boolean empty) {
              super.updateItem(item, empty);
              if (!empty) {
                // Use a SimpleDateFormat or similar in the format method
                setText(sdf.format((Date)item));
              } else {
                setText(null);
              }
            }
          };
        }
      });
    } else if (column.getFieldType()==FxTableField.TYPE_MONEY) {
      col.setCellFactory(new Callback<TableColumn, TableCell>() {
        @Override
        public TableCell call(TableColumn param) {
          return new TableCell() {
            @Override
            protected void updateItem(Object item, boolean empty) {
              super.updateItem(item, empty);
              if (!empty) {
                setText(nfmt.format((Double)item));
                setAlignment(Pos.TOP_RIGHT);
                if ((Double)item<0) {
                  setTextFill(Color.RED);
                } else {
                  setTextFill(Color.BLACK);
                }
              } else {
                setText(null);
              }
            }
          };
        }
      });
    } else if (column.getFieldType()==FxTableField.TYPE_HTML) {
      col.setCellFactory(new Callback<TableColumn, TableCell>() {
        @Override
        public TableCell call(TableColumn param) {
          return new TableCell() {
            @Override
            protected void updateItem(Object item, boolean empty) {
              super.updateItem(item, empty);
              if (!empty) {
                String html = (String)item;
                WebView webView = new WebView();
                webView.setPrefWidth(200);
                webView.setPrefHeight(50);
                WebEngine engine = webView.getEngine();
                engine.loadContent(html);
                setGraphic(webView);
              } else {
                setGraphic(null);
              }
            }
          };
        }
      });
    }
    column.setControl(col);
    table.getColumns().add(col);
    return col;
  }
  
}
